import os
from pathlib import *
import gettext
import locale

LOCALE_ZH_CH = "zh_CN"
LOCALE_EN_US = "en-US"
LOCALE_AUTO = 'auto'


def get_org_text(text):
    return text


g_get_text_fun = get_org_text


def get_locale_text(text):
    return g_get_text_fun(text)


def set_locale(language_file, language_dir, language_type):
    global g_get_text_fun
    language, encoding = locale.getdefaultlocale()
    if language_type in [LOCALE_ZH_CH, LOCALE_EN_US]:
        pass
    else:
        if language in [LOCALE_ZH_CH, LOCALE_EN_US]:
            language_type = language
        else:
            language_type = LOCALE_EN_US
    try:
        g_get_text_fun = gettext.translation(language_file, language_dir, [language_type, language_type]).gettext
    except FileNotFoundError as identifier:
        g_get_text_fun = get_org_text


if __name__ == '__main__':
    with open(Path(os.getcwd(), 'language.pot'), 'rb') as f:
        data = f.read()
    with open(Path(os.getcwd(), 'language.pot'), 'wb') as f:
        data = data.decode('gbk')
        data = data.replace("charset=cp936", "charset=UTF-8")
        data = data.encode('utf-8')
        f.write(data)
